package perform.mappings;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

public class TypeMappings {
	
	private static Map<Class<?>, String> types = new HashMap<Class<?>, String>();
	
	static{
		/* Numeric types */
		types.put(byte.class, "tinyint");
		types.put(Byte.class, "tinyint");
		types.put(short.class, "smallint");
		types.put(Short.class, "smallint");
		types.put(int.class, "int");
		types.put(Integer.class, "int");
		types.put(long.class, "bigint");
		types.put(Long.class, "bigint");
		types.put(float.class, "float");
		types.put(Float.class, "float");
		types.put(double.class, "double");
		types.put(Double.class, "double");
		types.put(BigDecimal.class, "decimal");
		/* Boolean */
		types.put(boolean.class, "bool");
		types.put(Boolean.class, "bool");
		/* String and char types */
		types.put(char.class, "char");
		types.put(Character.class, "char");
		types.put(String.class, "varchar");
		types.put(Blob.class, "blob");
		/* Date and time types */
		types.put(java.util.Date.class, "datetime");
		types.put(Date.class, "date");
		types.put(Time.class, "time");
		types.put(Timestamp.class, "timestamp");
		/* Stream and reader */
		types.put(InputStream.class, "mediumblob");
		types.put(Reader.class, "mediumtext");
		/* Arrays */
		types.put(char[].class, "char ");
		types.put(Character[].class, "char ");
		types.put(CharSequence.class, "varchar");
		types.put(byte[].class, "varbinary");
		types.put(Byte[].class, "varbinary");
		
	}
	
	public static String type(Class<?> javaType){
		if(javaType.isEnum()){
			return "enum";
		}
		return types.get(javaType);
	}
	public static int length(String dataType){
		if(dataType.equals("varchar") || dataType.equals("varbinary")){
			return 255;
		} else if(dataType.equals("char") || dataType.equals("binary")){
			return 1;
		} else if(dataType.equals("char ")){
			return 50;
		}
		return 0;
	}
	
}
